//
//  SBPauseOrPlayView.swift
//  sh
//
//  Created by xgp on 2017/5/8.
//  Copyright © 2017年 xyz. All rights reserved.
//

import Foundation
import UIKit
import SnapKit
protocol SBPauseOrPlayViewDelegate {
    /**
     暂停和播放视图和状态
     @param pauseOrPlayView 暂停或者播放视图
     @param state 返回状态
     */
    func pauseOrPlayView(pauseOrPlayView:SBPauseOrPlayView, withState state:Bool)
}

class SBPauseOrPlayView: UIView {
    var imageBtn:UIButton?
    var delegate:SBPauseOrPlayViewDelegate?
    var state:Bool = true
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func draw(_ rect: CGRect) {
        self.imageBtn = UIButton(type: .custom)
        self.imageBtn?.setImage(UIImage(named:"play"), for: .normal)
        self.imageBtn?.showsTouchWhenHighlighted = true
        
        self.imageBtn?.setImage(UIImage(named:"pause"), for: .selected)
        self.imageBtn?.addTarget(self, action: #selector(handleImageTapAction(button:)), for: .touchUpInside)
        self.addSubview(imageBtn!)
        imageBtn?.snp.makeConstraints({ (make) in
            make.edges.equalTo(self)
        })
    }
    
    func handleImageTapAction(button:UIButton) -> Void {
        button.isSelected = !button.isSelected
        state = button.isSelected
        guard let _ = delegate else {
            return
        }
        self.delegate?.pauseOrPlayView(pauseOrPlayView: self, withState: state)
    }
}
